package com.example.photography.adapter.product;

import android.content.Context;
import android.view.View;
import android.widget.TextView;


import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.photography.R;
import com.example.photography.adapter.MyBaseAdapter;
import com.example.photography.adapter.group.GroupPhotoAdatper;
import com.example.photography.bean.GroupsBean;
import com.example.photography.bean.UrlBean;
import com.example.photography.ui.MyGridView;
import com.example.photography.util.UIUtils;
import com.example.photography.view.CustomRoundAngleImageView;


import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GroupAdapter extends MyBaseAdapter {

    private List<UrlBean> urlBeans;

    public GroupAdapter(List list, Context context) {
        super(list, context);
    }

    @Override
    public View myGetView(int position, View convertView) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_group, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        GroupsBean groupBean = (GroupsBean) list.get(position);
        Glide.with(Objects.requireNonNull(mContext))
                .load(groupBean.getHead_image())
                .thumbnail(Glide.with(UIUtils.getContext()).load(R.drawable.loading))
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                .into(viewHolder.cvHead);
        viewHolder.tvStyle.setText("#" + groupBean.getStyle() + "#");
        viewHolder.tvPeople.setText(groupBean.getPeople_num() + "人");
        viewHolder.tvProduct.setText(groupBean.getProduct_num() + "件作品");
        viewHolder.tvAdministrator.setText("圈子由" + groupBean.getName() + "管理");
        viewHolder.tvDetails.setText(groupBean.getDetail());
        urlBeans = JSONObject.parseArray(groupBean.getProducts(), UrlBean.class);
        GroupPhotoAdatper groupPhotoAdatper = new GroupPhotoAdatper(urlBeans,mContext);
        viewHolder.gvProduct.setAdapter(groupPhotoAdatper);
        return convertView;
    }


    static
    class ViewHolder {
        @BindView(R.id.cv_head)
        CustomRoundAngleImageView cvHead;
        @BindView(R.id.tv_style)
        TextView tvStyle;
        @BindView(R.id.tv_people)
        TextView tvPeople;
        @BindView(R.id.tv_product)
        TextView tvProduct;
        @BindView(R.id.tv_administrator)
        TextView tvAdministrator;
        @BindView(R.id.tv_details)
        TextView tvDetails;
        @BindView(R.id.gv_product)
        MyGridView gvProduct;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
